package com.ruoyi.rescue.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.rescue.api.domain.CheRescue;
import com.ruoyi.system.api.RemoteUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.user.api.RemoteCarService;
import com.ruoyi.user.api.domain.CheCar;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.rescue.mapper.CheRescueMapper;
import com.ruoyi.rescue.service.ICheRescueService;

/**
 * 救援记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-03
 */
@Service
public class CheRescueServiceImpl implements ICheRescueService 
{
    @Autowired
    private CheRescueMapper cheRescueMapper;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private RemoteCarService remoteCarService;


    /**
     * 查询救援记录
     * 
     * @param id 救援记录主键
     * @return 救援记录
     */
    @Override
    public CheRescue selectCheRescueById(Long id)
    {
        return cheRescueMapper.selectCheRescueById(id);
    }

    /**
     * 查询救援记录列表
     * 
     * @param cheRescue 救援记录
     * @return 救援记录
     */
    @Override
    public List<CheRescue> selectCheRescueList(CheRescue cheRescue)
    {
        List<CheRescue> cheRescues = cheRescueMapper.selectCheRescueList(cheRescue);
        cheRescues.forEach(e->{
            Long userId = e.getUserId();
            Long carId = e.getCarId();
            AjaxResult info = remoteUserService.getInfo(userId, null);
            Object o = info.get(AjaxResult.DATA_TAG);
            if (o==null){
            return;
            }
            SysUser sysUser = JSON.parseObject(JSON.toJSON(o).toString(), SysUser.class);
            e.setSysUser(sysUser);
            AjaxResult info1 = remoteCarService.getInfo(carId, null);
            Object o1 = info1.get(AjaxResult.DATA_TAG);
            if (o1==null){
                return;
            }
            CheCar cheCar = JSON.parseObject(JSON.toJSON(o1).toString(), CheCar.class);
            e.setCheCar(cheCar);
        });
        return cheRescues;
    }

    /**
     * 新增救援记录
     * 
     * @param cheRescue 救援记录
     * @return 结果
     */
    @Override
    public int insertCheRescue(CheRescue cheRescue)
    {
        return cheRescueMapper.insertCheRescue(cheRescue);
    }

    /**
     * 修改救援记录
     * 
     * @param cheRescue 救援记录
     * @return 结果
     */
    @Override
    public int updateCheRescue(CheRescue cheRescue)
    {
        return cheRescueMapper.updateCheRescue(cheRescue);
    }

    /**
     * 批量删除救援记录
     * 
     * @param ids 需要删除的救援记录主键
     * @return 结果
     */
    @Override
    public int deleteCheRescueByIds(Long[] ids)
    {
        return cheRescueMapper.deleteCheRescueByIds(ids);
    }

    /**
     * 删除救援记录信息
     * 
     * @param id 救援记录主键
     * @return 结果
     */
    @Override
    public int deleteCheRescueById(Long id)
    {
        return cheRescueMapper.deleteCheRescueById(id);
    }
}
